﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Xml;
using System.Xml.Serialization;

namespace RecursosHumanos
{
    public partial class frmVeiculo : Form
    {
        WBGEntities bd = new WBGEntities();

        public frmVeiculo()
        {
            InitializeComponent();
        }

        private void frmFilme_Load(object sender, EventArgs e)
        {
            AtualizaGrid();
        }

        private void AtualizaGrid()
        {
            Cursor.Current = Cursors.WaitCursor;

            grd.DataSource = from x in bd.Veiculo
                             select new { ID=x.idVeiculo, Marca=x.Marca.nome, Modelo=x.VeiculoModelo.nome, Cor=x.cor, /*Placa=x.placa,*/ Ativo=x.ativo };//bd.Filme;
            
            FormataGrid();

            Cursor.Current = Cursors.Default;
        }

        private void FormataGrid()
        {
            //grd.Columns[0].HeaderText = "ID";
            //grd.Columns[1].HeaderText = "Marca";
            //grd.Columns[2].HeaderText = "Modelo";
            //grd.Columns[3].HeaderText = "Cor";
            //grd.Columns[4].HeaderText = "Placa";
            //grd.Columns[5].HeaderText = "Ativo";
        }

        private void tsbSair_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void grd_Click(object sender, EventArgs e)
        {
            try
            {
                //grd.CurrentRow.Cells[0].ToolTipText = FilmeGenero.Find(Convert.ToInt32(grd.CurrentRow.Cells["genero"].Value.ToString()));
            }
            catch { }
        }

        private void tsbEditar_Click(object sender, EventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(grd.CurrentRow.Cells["ID"].Value);
                var frm = new frmVeiculo_(Request.Alterar);
                frm.veiculo = bd.Veiculo.FirstOrDefault(x => x.idVeiculo == id);
                frm.ShowDialog();
                AtualizaGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message, "Erro ao alterar", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Deseja realmente excluir?", "Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                    return;

                int id = Convert.ToInt32(grd.CurrentRow.Cells["idVeiculo"].Value);
                var reg = bd.Veiculo.FirstOrDefault(x => x.idVeiculo == id);
                bd.DeleteObject(reg);
                bd.SaveChanges();
                AtualizaGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.Message, "Erro ao excluir", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void grd_DoubleClick(object sender, EventArgs e)
        {
            tsbEditar_Click(sender, e);
        }

        private void tsbSair_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmBairros_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
            {
                tsbSair_Click(sender, new EventArgs());
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            var frm = new frmVeiculo_(Request.Inserir);
            frm.ShowDialog();
            AtualizaGrid();
        }
    }
}
